<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<?php $this->beginBlock('title'); ?>广告管理<?php $this->endBlock();?>
<?php $this->beginBlock('breadcrumb'); ?>
<li class="active"><a href="<?php echo \Yii::$app->urlManager->createUrl('/advertise/index')?>">广告管理</a></li>
<?php $this->endBlock();?>

<div class="row">
	<div class="col-sm-12">
		<section class="panel">
			<div class="panel-body">
				<form class="form-inline" role="form" action="<?php echo \Yii::$app->urlManager->createUrl('/advertise/index')?>" method="get">
					<div class="form-group">
						<label class="sr-only" for="key">关键字</label>
                        <input type="text" class="form-control" id="key" name="key" value="<?=\Yii::$app->request->get('key');?>" placeholder="标题模糊查询">
                    </div>
					<button type="submit" class="btn btn-info"><i class="fa fa-search"></i> 查询</button>
					<a href="<?php echo \Yii::$app->urlManager->createUrl('/advertise/edit');?>"><button type="button" class="btn btn-success"><i class="fa fa-plus"></i> 添加广告 </button></a>
				</form>
			</div>
			<table class="table table-hover">
				<thead>
					<tr>
						<th>#</th>
						<th>图片</th>
						<th>标题</th>
						<th>地址</th>
						<th>排序值 &nbsp;&nbsp;<i data-val="<?php echo \Yii::$app->request->get('sort');?>" data-sort="sort" class="sort fa <?php if(\Yii::$app->request->get('sort')=='desc'):?>fa-sort-amount-desc<?php elseif(\Yii::$app->request->get('sort')=='asc'):?>fa-sort-amount-asc<?php else:?>fa-arrows-v<?php endif;?>" style="cursor: pointer;"></i></th>
						<th>创建日期</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ($models as $item):?>
					<tr>
						<td><?= $item['id']?></td>
						<td><img src="<?= filepath($item['img_path']);?>" style="width:35px; height:35px;" /></td>
						<td><?= truncate(Html::encode($item['title']),25);?></td>
						<td><?= truncate(Html::encode($item['url']),30);?></td>
						<td><?= $item['sort'];?></td>
						<td><?= date('Y-m-d',$item['created_at']);?></td>
						<td>
							<a class="btn btn-sm btn-primary" href="<?=\Yii::$app->urlManager->createUrl(['/advertise/edit','id'=>$item['id']]);?>"><i class="icon-pencil"></i> 编辑</a>
							<a class="btn btn-sm btn-danger" data-toggle="modal" href="#DeleteConfirmModal" onclick="deleteConfirm(<?=$item['id'];?>)"><i class="icon-trash"></i> 删除</a>
						</td>
					</tr>
					<?php endforeach;?>
				</tbody>
			</table>
			<div class="text-center">
				<?php
                echo LinkPager::widget([
                    'pagination' => $pages,
                ]);
                ?>
			</div>
		</section>
	</div>
</div>

<!-- Modal -->
<div class="modal fade" id="DeleteConfirmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除确认</h4>
            </div>
            <div class="modal-body">
                <p>您确定要删除此广告吗？</p>
            </div>
            <div class="modal-footer">
            	<form method="post" action="<?php echo \Yii::$app->urlManager->createUrl('/advertise/delete');?>">
            	   <input type="hidden" name="_csrf" id="_csrf" value="<?=\Yii::$app->request->csrfToken; ?>" />
	            	<input type="hidden" id="advertise_id" name="advertise_id" value="" >
	                <button class="btn btn-danger" type="submit"> 确认</button>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- modal -->

<?php $this->beginBlock('script'); ?>
<script type="text/javascript">
//删除确认
function deleteConfirm(id){
	$("#advertise_id").val(id);
}

//排序
$(".sort").click(function() {
    var url = "<?php echo \Yii::$app->urlManager->createUrl(['/advertise/index','page' => \Yii::$app->request->get('page'), 'key' => \Yii::$app->request->get('key',''), 'kind' => \Yii::$app->request->get('kind','')])?>";
    var val = $(this).attr('data-val');
    var sort = $(this).attr('data-sort');
    if (val != 'desc') {
        url += '&'+sort+'=desc';
    } else {
        url += '&'+sort+'=asc';
    }
    window.location.href = url;
});
</script>
<?php $this->endBlock('script'); ?>